#include "buffer.hpp"
#include "buffer_alloc.hpp"

namespace mina
{
	BufferAlloc::BufferAlloc(int block_size)
		: _block_size(block_size)
	{
	}

	int BufferAlloc::buffer_size() const
	{
		return (_block_size - sizeof(Buffer));
	}

	PoolBufferAlloc::PoolBufferAlloc(MemoryPool & alloc)
		: BufferAlloc(alloc.block_size()),
		  _alloc(alloc)
	{
	}

	PoolBufferAlloc::~PoolBufferAlloc()
	{
	}

	Buffer * PoolBufferAlloc::allocate()
	{
		void * ptr = _alloc.allocate();
		return ((NULL == ptr)? NULL: new (ptr) Buffer(_alloc.block_size()));
	}

	void PoolBufferAlloc::deallocate(Buffer & buffer)
	{
		void * ptr = (void*) &buffer;
		_alloc.deallocate(ptr);
	}

	CircleBufferAlloc::CircleBufferAlloc(CircleBuffer & alloc)
		: BufferAlloc(alloc.block_size()),
		  _alloc(alloc)
	{
	}

	CircleBufferAlloc::~CircleBufferAlloc()
	{
		// SAFE_DELETE(_alloc);
	}

	Buffer * CircleBufferAlloc::allocate()
	{
		void * ptr = _alloc.allocate();
		return ((NULL == ptr)? NULL: new (ptr) Buffer(_alloc.block_size()));
	}

	void CircleBufferAlloc::deallocate(Buffer & buffer)
	{
		void * ptr = (void*) &buffer;
		_alloc.deallocate(ptr);
	}
}

